package com.lyn.plugin_scan.mvp;

import android.annotation.SuppressLint;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import androidx.annotation.NonNull;
import com.lyn.image.ImageNative;
import com.lyn.image.Rotate;
import com.lyn.plugin.utils.LogUtil;
import com.lyn.plugin.PluginHelper;
import com.lyn.plugin.annotation.PluginLauncher;
import com.lyn.plugin.entity.PluginParameter;
import com.lyn.plugin.interfaces.ProxyActivityInterface;
import com.lyn.plugin_scan.R;
import com.lyn.plugin_scan.databinding.ActivityMainBinding;
import com.lyn.qrcode.Data;
import com.lyn.qrcode.QrCodeHelper;
import java.lang.ref.WeakReference;
import java.util.List;

@SuppressLint("NonConstantResourceId")
@PluginLauncher(pluginName= "扫码",logoId = R.drawable.scan)
public class MainActivity implements ProxyActivityInterface, Camera.PreviewCallback {
    private ActivityMainBinding binding;
    private PluginParameter actParameter;
    private ImageNative imageNative;

    @Override
    public void onLifeCycle(String s, Object... objects) {
        if (s.equals("onCreate")){
            this.actParameter = (PluginParameter) objects[0];
            View view = PluginHelper.getView(actParameter, R.layout.activity_main);
            binding=ActivityMainBinding.bind(view);
            actParameter.getActivity().setContentView(view);
            init();
        }

    }

    private void init() {
        imageNative=new ImageNative();
        binding.cameraSurface.setPreviewCallback(this);
        binding.cameraSurface.setOnClickListener(v -> {
            handler.removeMessages(1);
            handler.obtainMessage(1,camera).sendToTarget();
        });
    }



    private Camera camera;
    private final MyHandler handler=new MyHandler(this);

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if (actParameter.getActivity().isFinishing()){
            return;
        }
        Camera.Parameters cameraParameters = camera.getParameters();
        Camera.Size size = cameraParameters.getPreviewSize();
        this.camera=camera;
        byte[] rotateNV21 = imageNative.rotateNV21(data, size.width, size.height, Rotate.ROTATE_90);
        List<Data> dataList = QrCodeHelper.getInstance().scan(rotateNV21, size.height, size.width);
        if (dataList.isEmpty()){
            return;
        }
        StringBuilder sb=new StringBuilder();
        for (Data qrCodeData:dataList){
            sb.append("二维码(");
            sb.append(qrCodeData.getQrCode());
            sb.append(")");
            sb.append("\n");
        }
        binding.log.setText(sb.toString());


    }


    static class MyHandler extends Handler{
        WeakReference<MainActivity> weakReference;

        public MyHandler(MainActivity mainActivity) {
            this.weakReference = new WeakReference<>(mainActivity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Camera camera= (Camera) msg.obj;
            if (camera!=null){
                camera.autoFocus((success, camera1) -> {
                    LogUtil.e("对焦:"+success);
                });
            }

        }
    }
}